package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.statistics.mapper.MemberRecommendationSumMapper;
import com.newtouch.bxzs.business.statistics.mapper.OrganizationMapper;
import com.newtouch.bxzs.business.statistics.mapper.OrganizationRecommendationSumMapper;
import com.newtouch.bxzs.business.statistics.model.bo.MemberRecommendationSumBO;
import com.newtouch.bxzs.business.statistics.model.bo.OrganizationRecommendationSumBO;
import com.newtouch.bxzs.business.statistics.model.vo.MemberRecommendationSumVO;
import com.newtouch.bxzs.business.statistics.model.vo.OrganizationRecommendationSumVO;
import com.newtouch.bxzs.business.statistics.service.MemberRecommendationSumService;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 成员建议书出单率汇总
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/26 15:36</td>
 * <td>lingshouyi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lingshouyi
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class MemberRecommendationSumServiceImpl implements MemberRecommendationSumService {

    @Autowired
    private MemberRecommendationSumMapper memberRecommendationSumMapper;

    @Autowired
    private OrganizationRecommendationSumMapper organizationRecommendationSumMapper;

    @Autowired
    private PersonalMapper personalMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public IPage<MemberRecommendationSumBO> getMemberRecommendationSumList(MemberRecommendationSumVO memberRecommendationSumVO) {

        if (BeanUtil.isEmpty(memberRecommendationSumVO)) {
            return null;
        }
        /*根据机构编码获取该机构业务员*/
        List<String> userIdList = new ArrayList<>();
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("USER_ORGANIZATION_CODE", memberRecommendationSumVO.getOrganizationCode());
        /*获取成员工号*/
        for (User user : personalMapper.selectList(qw)) {
            userIdList.add(user.getUserId());
        }
        /*分页*/
        Page<MemberRecommendationSumBO> page = new Page<>(memberRecommendationSumVO.getCurrentPage(), memberRecommendationSumVO.getPageSize());
        IPage<MemberRecommendationSumBO> memberRecommendationSumList = memberRecommendationSumMapper.getMemberRecommendationSum(page, userIdList, DateUtil.beginOfDay(memberRecommendationSumVO.getStartTime()),DateUtil.endOfDay(memberRecommendationSumVO.getEndTime()));
        /*排名*/
        Integer n = 1;
        for (MemberRecommendationSumBO recommendationSumBO : memberRecommendationSumList.getRecords()) {
            recommendationSumBO.setRank(n);
            n++;
        }
        return memberRecommendationSumList;
    }

    @Override
    public IPage<OrganizationRecommendationSumBO> getOrganizationRecommendationSumList(OrganizationRecommendationSumVO organizationRecommendationSumVO) {
        if (BeanUtil.isEmpty(organizationRecommendationSumVO)) {
            return null;
        }
        if (CollectionUtils.isEmpty(organizationRecommendationSumVO.getOrganizationCodeList())){
           String childOrganizationCodeStr = organizationMapper.selectChildOrganizationCodeStr(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserOrganizationCode());
            String[] childOrgCodeArr = childOrganizationCodeStr.split(",");
            organizationRecommendationSumVO.setOrganizationCodeList(Arrays.asList(childOrgCodeArr));
        }
        /*分页*/
        Page<OrganizationRecommendationSumBO> page = new Page<>(organizationRecommendationSumVO.getCurrentPage(), organizationRecommendationSumVO.getPageSize());
        if (CollectionUtils.isEmpty(organizationRecommendationSumVO.getOrganizationCodeList())) {
            List<String> organizationCodeList = new ArrayList<>();
            organizationCodeList.add(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserOrganizationCode());
            organizationRecommendationSumVO.setOrganizationCodeList(organizationCodeList);
        }
        return organizationRecommendationSumMapper.getOrganizationRecommendationSumList(page, organizationRecommendationSumVO.getOrganizationCodeList(),DateUtil.beginOfDay(organizationRecommendationSumVO.getStartTime()),DateUtil.endOfDay(organizationRecommendationSumVO.getEndTime()));
    }


}
